package org.br.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.entity.Djlinkrelation;
import org.br.erp.base.mapper.DjlinkrelationMapper;
import org.br.erp.base.mapper.DjlxglMapper;
import org.br.erp.base.service.IDjlinkrelationService;
import org.br.erp.base.vo.linkNodes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 单据关联关系管理
 * @Author: jeecg-boot
 * @Date:   2023-05-08
 * @Version: V1.0
 */
@Service
public class DjlinkrelationServiceImpl extends ServiceImpl<DjlinkrelationMapper, Djlinkrelation> implements IDjlinkrelationService {


    @Autowired
    private DjlinkrelationMapper djlinkrelationMapper;

    @Autowired
    private DjlxglMapper djlxglMapper;
    /**
     * 根据单据类型code查询当前上下游关系的单据信息
     * @param billid
     * @param djlxcode
     * @return
     */
    public List<linkNodes> getCurrentLinkNodeInfo(String billid, String djlxcode){

        List<linkNodes> res=new ArrayList<>();

        linkNodes curlinkNodes= new linkNodes();
        curlinkNodes.setCurIndex(0);
        curlinkNodes.setCurlxcode(djlxcode);
        //todo 1 先去查找上游单据信息
        QueryWrapper<Djlinkrelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nextdjlxcode",djlxcode);
        List<Djlinkrelation> djlinkrelations = djlinkrelationMapper.selectList(queryWrapper);
        if(djlinkrelations!=null && djlinkrelations.size()>0){
            for (Djlinkrelation djlinkrelation : djlinkrelations) {

            }
        }
        //todo 2 查找下游单据信息
        queryWrapper.clear();
        queryWrapper.eq("postdjlxcode",djlxcode);
        List<Djlinkrelation> nextDjlinkrelation = djlinkrelationMapper.selectList(queryWrapper);
        if(nextDjlinkrelation!=null && nextDjlinkrelation.size()>0){

        }

        return null;
    }


    public List<linkNodes> getPostLinkNodes(List<linkNodes>res,String djlxcode,String id){
        if(djlxcode.equalsIgnoreCase("")){
            return res;
        }
        QueryWrapper<Djlinkrelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("nextdjlxcode",djlxcode);
        List<Djlinkrelation> djlinkrelations = djlinkrelationMapper.selectList(queryWrapper);
        if(djlinkrelations!=null && djlinkrelations.size()>0){
            for (Djlinkrelation djlinkrelation : djlinkrelations) {
                //获取到上游单据类型编号
                String postdjlxcode = djlinkrelation.getPostdjlxcode();

                List<Map> selectdjlinkrelation = djlinkrelationMapper.selectdjlinkrelation(postdjlxcode, djlxcode, djlinkrelation.getPostdjlinkcode(), djlinkrelation.getNextdjlinkcode(), id);

                if(selectdjlinkrelation!=null && selectdjlinkrelation.size()>0){
                    Map map1 = selectdjlinkrelation.get(0);
                    String billcode = map1.get("billcode").toString();
                    String id1 = map1.get("id").toString();
                    linkNodes currentNode = new linkNodes();
                    currentNode.setCurlxcode(postdjlxcode);
                    currentNode.setNodeid(id1);
                    currentNode.setNodecode(billcode);
                    currentNode.setNextlinknodeid(id);
                    res.add(currentNode);
                    getPostLinkNodes(res,postdjlxcode,id1);
                }else{
                    //不存在上游单据，则直接返回

                }
                //select billcode from postdjlxcode where exists(selet 1 from djlxcode where djlxcode.fromitemid = postdjlxcode.id and djlxcode .id = id)
            }

            return res;
        }

        return res;
    }

    /**
     * 获取后置单据跟踪数据
     * @param res
     * @param djlxcode
     * @param id
     * @return
     */
    public List<linkNodes> getNextLinkNodes(List<linkNodes> res,String djlxcode,String id){
        if(djlxcode.equalsIgnoreCase("")){
            return res;
        }
        QueryWrapper<Djlinkrelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("postdjlxcode",djlxcode);
        List<Djlinkrelation> djlinkrelations = djlinkrelationMapper.selectList(queryWrapper);
        if(djlinkrelations!=null && djlinkrelations.size()>0){
            for (Djlinkrelation djlinkrelation : djlinkrelations) {
                //获取到下游单据类型编号
                String nextdjlxcode = djlinkrelation.getNextdjlinkcode();

                List<Map> selectdjlinkrelation = djlinkrelationMapper.selectdjlinkrelation(nextdjlxcode, djlxcode, djlinkrelation.getPostdjlinkcode(), djlinkrelation.getNextdjlinkcode(), id);

                if(selectdjlinkrelation!=null && selectdjlinkrelation.size()>0){
                    Map map1 = selectdjlinkrelation.get(0);
                    String billcode = map1.get("billcode").toString();
                    String id1 = map1.get("id").toString();
                    linkNodes currentNode = new linkNodes();
//                    currentNode.setCurlxcode(postdjlxcode);
//                    currentNode.setNodeid(id1);
//                    currentNode.setNodecode(billcode);
//                    currentNode.setNextlinknodeid(id);
//                    res.add(currentNode);
//                    getPostLinkNodes(res,postdjlxcode,id1);
                }else{
                    //不存在上游单据，则直接返回

                }
                //select billcode from postdjlxcode where exists(selet 1 from djlxcode where djlxcode.fromitemid = postdjlxcode.id and djlxcode .id = id)
            }

            return res;
        }
        return res;
    }
}
